package com.exam.controller;

import com.exam.entity.Student;
import com.exam.service.ReservationService;
import com.exam.util.ZLConstants;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.json.MappingJackson2JsonView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@RestController
public class ReserveController {

    @Resource
    private ReservationService reservationService;

    @RequestMapping("/reserve")
    public Object reserve(String op,String eid,String eids,String sno,HttpServletRequest req){
        if(op!=null && !"".equals(op)){
            if("currStuInDate".equals(op)){
                Student stu = (Student) req.getSession().getAttribute(ZLConstants.USER);
                List<Map<String,Object>> res =reservationService.getReservationPerStuInDate(stu.getSno());
                return res;
            }else if("currStuOutOfDate".equals(op)){
                Student stu = (Student) req.getSession().getAttribute(ZLConstants.USER);
                List<Map<String,Object>> res = reservationService.getReservationPerStuOutOfDate(stu.getSno());
                return res;
            }else if("reserve".equals(op)){
                Student stu = (Student) req.getSession().getAttribute(ZLConstants.USER);
                int res = reservationService.reserveExam(Integer.parseInt(eid),stu.getSno());
                if(res>0){
                    return "1";
                }else{
                    return "0";
                }
            }else if("remove".equals(op)){
                int result = 0;
                Student stu = (Student) req.getSession().getAttribute(ZLConstants.USER);
                String[] oids = eids.split(",");
                for (String oid : oids){
                    if(reservationService.removeReservation(oid,stu.getSno())>0){
                        result = 1;
                    }
                }
                return result;
            }else if("cancel".equals(op)){
                int result = 0;
                Student stu = (Student) req.getSession().getAttribute(ZLConstants.USER);
                String[] oids = eids.split(",");
                for (String oid : oids){
                    if(reservationService.cancelReservation(oid,stu.getSno())>0){
                        result = 1;
                    }
                }
                return result;
            }else if("allOutOfDate".equals(op)){
                Student stu = (Student) req.getSession().getAttribute(ZLConstants.USER);
                List<Map<String,Object>> res = reservationService.getReservationPerStuOutOfDate(stu.getSno());
                return res;
            }else if("allInDate".equals(op)){
                Student stu = (Student) req.getSession().getAttribute(ZLConstants.USER);
                List<Map<String,Object>> res = reservationService.getReservationPerStuInDate(stu.getSno());
                return res;
            }else if("adminAllOutOfDate".equals(op)){
                List<Map<String,Object>> res = reservationService.getAllReservationOutOfDate();
                return res;
            }else if("adminAllInDate".equals(op)){
                List<Map<String,Object>> res = reservationService.getAllReservationInDate();
                return res;
            }
        }
        return null;
    }
}
